﻿#pragma once
#include "AsGraphicsBasics.h"
#include "AsCall.h"

class AS_GRAPHICS_API AsGraphicsObject : public QObject {
public:
	AsGraphicsObject(QObject* parent = nullptr);
	virtual ~AsGraphicsObject();

private:
	AsGraphics* mpGraphics;

public:
	virtual void create(AsGraphics* graphics, QObject* parent);
	virtual void destroy();

protected:
	template <typename _GraphicsType, typename _PrivateType>
	void StdCreate(_GraphicsType* host, _PrivateType*& mp, AsGraphics* graphics, QObject* parent) {
		if (graphics == nullptr) return;
		if (mp) destroy();
		mp = new _PrivateType(host, graphics);
		AsGraphicsObject::create(graphics, parent);
	}
	template <typename _PrivateType>
	void StdDestroy(_PrivateType*& mp) {
		if (mp) {
			delete mp;
			mp = nullptr;
			AsGraphicsObject::destroy();
		}
	}

public:
	template <typename _Slot>
	void guiCall(_Slot slot) {
		AsCall::execute((QObject*)mpGraphics, slot);
	}
	template <typename _Slot>
	void guiBlockCall(_Slot slot) {
		AsCall::execute((QObject*)mpGraphics, slot, Qt::BlockingQueuedConnection);
	}

};
